package ltd.dujiabao.capability.adapter.driven.persistence.capmng.newmodulespec;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ltd.dujiabao.capability.adapter.driven.persistence.passthrough.PassThroughService;
import ltd.dujiabao.capability.common.dto.Result;
import ltd.dujiabao.capability.common.exception.BusinessException;
import ltd.dujiabao.capability.domain.capmng.sync.newmodulespec.NewModuleSpec;
import ltd.dujiabao.capability.domain.capmng.sync.newmodulespec.NewModuleSpecRepository;

/**
 * @author : DuJiabao
 * @Project : ddd-capability
 * @Time : 2024/4/11 20:57
 * @Desc :
 */
@Repository
public class NewModuleSpecRepositoryImpl implements NewModuleSpecRepository {

    @Autowired
    private PassThroughService passThroughService;

    @Override
    public NewModuleSpec findNewModuleSpec(Long devId) {
        Result<NewModuleSpecDTO> result = passThroughService.findNewModuleSpec(devId);
        if (!result.isSuccess()) {
            throw new BusinessException(result.getErrorCode());
        }

        List<String> moduleNames = result.getResult().getModules();
        Result<CapabilityDTO> capabilityResult = passThroughService.getCapability(devId, moduleNames);
        if (!capabilityResult.isSuccess()) {
            throw new BusinessException(capabilityResult.getErrorCode());
        }
        Map<String, Object> modules = capabilityResult.getResult().getCapability();
        return new NewModuleSpec(moduleNames, modules);
    }
}
